package com.kedacom.ctsp.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.kedacom.ctsp.starter.springboot.CommonsWebMvcConfigurer;
import com.kedacom.ctsp.starter.springboot.convert.FastJsonGenericHttpMessageConverter;
import com.kedacom.ctsp.starter.springboot.convert.FastJsonHttpMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;

import java.util.List;

import static com.alibaba.fastjson.serializer.SerializerFeature.*;

@Configuration
public class ExampleAutoConfig extends CommonsWebMvcConfigurer {

    private SerializerFeature[] features = {
            // 输出空置字段
            WriteMapNullValue,
            // list字段如果为null，输出为[]，而不是null
            WriteNullListAsEmpty,
            // 数值字段如果为null，输出为0，而不是null
            WriteNullNumberAsZero,
            // Boolean字段如果为null，输出为false，而不是null
            WriteNullBooleanAsFalse,
            // 字符类型字段如果为null，输出为""，而不是null
            WriteNullStringAsEmpty,
            DisableCircularReferenceDetect
    };

    @Autowired
    private FastJsonGenericHttpMessageConverter fastJsonGenericHttpMessageConverter;
    @Autowired
    private FastJsonHttpMessageConverter fastJsonHttpMessageConverter;

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        fastJsonGenericHttpMessageConverter.setFeatures(features);
        fastJsonHttpMessageConverter.setFeatures(features);
        super.configureMessageConverters(converters);
    }
}
